package com.qiaofang.applet.common.context;

import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * 使用 ThreadLocal 实现 QfContextProvider
 */
@Component
public class ThreadLocalQfContextProvider implements QfContextProvider {

    private final static ThreadLocal<QfContext> local = ThreadLocal.withInitial(QfContext::new);

    @Override
    public QfContext getContext() {
        return local.get();
    }

    @Override
    public QfContext setContext(QfContext context) {
        Assert.notNull(context, "context must not be null");
        local.set(context);
        return context;
    }

    @Override
    public void removeContext() {
        local.remove();
    }
}
